package com.base.configuration.action;

import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.enums.ConfigKey;
import com.base.common.persistent.model.Configuration;
import com.base.configuration.service.software.SoftwareService;
import com.cjy.common.bean.OpAttributes;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import javax.annotation.Resource;

import static com.base.common.GlobaConstants.*;

/**
 * 程序软件管理
 * 
 * @author Administrator
 *
 */

@Action("softwareManage")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class SoftwareManage extends AbstractBaseAction{

	private static final long serialVersionUID = 1L;
	
	private int bid;
	
	@Resource
	private SoftwareService softwareService;
	
	private String version;
	
	public int getBid() {
		return bid;
	}
	
	/**
	 * 获取app最新版本下载地址
	 * 
	 * @return
	 */
	public String getLatestVersion(){
		try {
			Configuration configuration = new Configuration();
			Configuration config = null;
			
			// 判断是哪种app
			if(bid == 0){
				configuration.setConfigKey(ConfigKey.CJY_ANDROID_VERSION.getValue().trim());
				config = softwareService.query(configuration);
				configuration.setConfigKey(ConfigKey.CJY_ANDROID_URL.getValue().trim());
				
			} else if (bid == 3) {
				configuration.setConfigKey(ConfigKey.CJY_IOS_VERSION.getValue().trim());
				config = softwareService.query(configuration);
				configuration.setConfigKey(ConfigKey.CJY_IOS_URL.getValue().trim());
			} else if (bid == 1){
				configuration.setConfigKey(ConfigKey.YGJ_ANDROID_VERSION.getValue().trim());
				config = softwareService.query(configuration);
				configuration.setConfigKey(ConfigKey.YGJ_ANDROID_URL.getValue().trim());
			} else if (bid == 4){
				configuration.setConfigKey(ConfigKey.YGJ_IOS_VERSION.getValue().trim());
				config = softwareService.query(configuration);
				configuration.setConfigKey(ConfigKey.YGJ_IOS_URL.getValue().trim());
			} else if(bid == 5){
				configuration.setConfigKey(ConfigKey.CJY_ANDROID_WEB_APP_VERSION.getValue().trim());
				config = softwareService.query(configuration);
				configuration.setConfigKey(ConfigKey.CJY_ANDROID_WEB_APP_URL.getValue().trim());
			}
			
			// 判断用户当前版本是否是最新版本
			if (config.getConfigValue().trim() != null && !config.getConfigValue().trim().equals("") && !config.getConfigValue().trim().equals(version.trim())) {
				actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result", new OpAttributes().add("url", softwareService.query(configuration).getConfigValue())));
			} else {
				actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
			}
			
		} catch (Exception e) {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	public String getVersion() {
		return version;
	}

	public void setBid(int bid) {
		this.bid = bid;
	}

	public void setVersion(String version) {
		this.version = version;
	}
	
}
